/**
 * @author helio
 */
if (!SALE.window) SALE.window = {}

if (!SALE.window.ManufacturerSelectWindow) {
	SALE.window.ManufacturerSelectWindow = Ext.extend(SALE.basicGridPanel, {
		
		layout: 'fit',
		initComponent: function(){
			this.detailsJson = {};
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'manufacturers',
					remoteSort: true,
					fields: ['id','code','name', 'tel', 'address', 'email', 'linkman']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
					
			// create the grid
			this.detailsGrid = new Ext.grid.GridPanel({
				region:'center',
				store: this.detailsStore,
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
				
				loadMask: true,
				stripeRows: true,
				border: true,
				autoExpandColumn: 'name',
								
				columns: [new Ext.grid.RowNumberer(), {
	           		header: "厂商编号",
	           		dataIndex: 'code',
	           		width: 60
	       		},{
	           		header: "厂商名称",
	           		dataIndex: 'name'
	        	}, {
					header: "厂商电话",
					dataIndex: 'tel',			
					width: 60
				}, {
	           		header: "厂商地址",
	           		dataIndex: 'address',
	           		width: 80
	        	}],
				
				sm: new Ext.grid.CheckboxSelectionModel(),
				
				tbar:[new Ext.form.ComboBox({
					id: 'mquerytype',
					tpl: comboxTpl,
				    triggerAction: 'all',
				    lazyRender:true,
				    forceSelection:true,
                 	selectOnFocus: true,
				    mode: 'local',
				    width: 100,
				    store: new Ext.data.ArrayStore({
				        id: 0,
				        fields: [
				            'code',
				            'name'
				        ],
				        data: [[1, '按编号查询'], [2, '按名称查询'], [3, '按电话查询'], [4, '按地址查询']]
				    }),
				    valueField: 'code',
				    displayField: 'name',
				    value:1
				}), '-', {
					id: 'mqueryvalue',
                	xtype:'trigger',
                    triggerClass: 'x-form-search-trigger',
                    width: 200,
                    onTriggerClick: this.queryManufacturer.createDelegate(this),
                    listeners: {
        				'show': this.queryManufacturer.createDelegate(this)
        			}
                }, '->', {
		            //text:'添加厂商',
		            tooltip:'添加厂商信息',
		            iconCls:'add',
					handler: this.showManufacturerEditWindow.createDelegate(this)
		        }],
                
                bbar: pagingBar,
                
                buttons: [{
					text: '选中',
					listeners: {
						"click": this.selectHandler.createDelegate(this)
					}
				},{
					text: '取消',
					listeners:{
						"click":this.cancelHandler.createDelegate(this)
					}
				}]
			});
			
			this.items = this.detailsGrid;
			
			SALE.window.ManufacturerSelectWindow.superclass.initComponent.call(this);
		},
		
		queryManufacturer : function() {
			var querytype = Ext.getCmp('mquerytype').getValue();
			var queryvalue = Ext.getCmp('mqueryvalue').getValue();
			var condition = {};
			if(querytype == 1) {
				condition.code = queryvalue;
			} else if(querytype == 2) {
				condition.name = queryvalue;
			} else if(querytype == 3) {
				condition.tel = queryvalue;
			} else if(querytype == 4) {
				condition.address = queryvalue;
			} 
			
			if(queryvalue){
				SALE.Action.getManufacturersListJson(this.initlizeDataHandler.createDelegate(this), condition);
			}
		},
		
		initlizeDataHandler : function(resultJson) {
			this.detailsJson['manufacturers'] = resultJson.manufacturers;
			this.detailsStore.load({params:{start:0, limit:25}});
		},
		
		selectHandler : function() {
			var recordList = this.detailsGrid.getSelectionModel().getSelections();
			if(recordList.length == 0) {
				SALE.Utils.showMessageBox('请先选择一条数据！');
			} else if(recordList.length > 1) {
				SALE.Utils.showMessageBox('每次只能选择一条数据，请确认只选中了一条数据！');
			} else {
				var record = recordList[0];
				if(SALE.window.showManufacturerSelectWindow.selectCallback) {					
					SALE.window.showManufacturerSelectWindow.selectCallback(record);
				}
				this.hide();
			}
		},
		
		cancelHandler : function() {
			this.hide();
		},
		
		showManufacturerEditWindow : function() {
			SALE.window.showManufacturerEditWindow(null, null, 'new');
		}
	});
	
	Ext.reg('manufacturerselectwindow', SALE.window.ManufacturerSelectWindow);
}

SALE.window.showManufacturerSelectWindow = function(selectCallback, condition) {
	SALE.window.showManufacturerSelectWindow.selectCallback = selectCallback;
	SALE.window.showManufacturerSelectWindow.condition = condition;
	if(!this.selectMfgrWin) {
		this.selectMfgrWin = new Ext.Window({
		 		
		 	title: '厂商信息查询',
			closable: true,
			//collapsible  : true,
			//maximizable: true,
			width: 600,
			height: 400,
			layout: 'fit',
			closeAction :'hide',
					
			items: {
				border: false,
				frame: true,
				xtype : "manufacturerselectwindow"
			},
			
			listeners: {
				'show': function(){
		    		if(SALE.window.showManufacturerSelectWindow.condition) {
		    			var condition = SALE.window.showManufacturerSelectWindow.condition;
		    			Ext.getCmp('mquerytype').setValue(condition.querytype);
						Ext.getCmp('mqueryvalue').setValue(condition.queryvalue);
						Ext.getCmp('mqueryvalue').show();
		    		}			    		
				}
			}
			
		});
	}
	this.selectMfgrWin.show();
}